//
//  MYVisitorView.swift
//  NewMicroblog
//
//  Created by apple on 2016/10/23.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MYVisitorView: UIView {
    
    var  closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 236/255, alpha: 1)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func btnClick(){
        closure!()
    }
    func setIconViewAndBackView(imageName:String?, title:String?){
        if let img = imageName, let tit = title {
            
            homeView.image = UIImage(named: img)
            detaiLabel.text = tit
            iconView.isHidden = true
        }else{
            ///设置动画
            setIconAni()
        }
    }
    
    private func setIconAni(){
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = M_PI * 2
        ani.duration = 20
        ani.isRemovedOnCompletion = false
        ani.repeatCount = MAXFLOAT
        iconView.layer.add(ani, forKey: nil)
        
    }
    
    ///设置界面
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(backView)
        addSubview(homeView)
        addSubview(detaiLabel)
        addSubview(leftBtn)
        addSubview(rightBtn)
        ///给子空间添加约束
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        backView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        homeView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        detaiLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(backView.snp_bottom).offset(10)
            make.width.equalTo(230)
        }
        leftBtn.snp_makeConstraints { (make) in
            make.left.equalTo(detaiLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.top.equalTo(detaiLabel.snp_bottom).offset(10)
        }
        rightBtn.snp_makeConstraints { (make) in
            make.size.top.equalTo(leftBtn)
            make.right.equalTo(detaiLabel)
        }
    
    }
    
    /// 创建动画视图
   lazy var iconView :UIImageView = {
        let icon = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
        return icon
    }()
    ///创建遮罩视图
   lazy var backView :UIImageView = {
        let backView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
        return backView
    }()
    /// 创建中间视图
   lazy var homeView: UIImageView = {
        let homeView = UIImageView(imageName: "visitordiscover_feed_image_house")
        return homeView
    }()
    /// 描述信息
   lazy var detaiLabel:UILabel = {
        let detailLabel = UILabel(title: "你好", fontSize: 14, textColor: UIColor.darkGray)
        detailLabel.textAlignment = NSTextAlignment.center
        detailLabel.numberOfLines = 0
        return detailLabel
    }()
    
    /// 登录按钮
   lazy var leftBtn:UIButton = {
        let leftBtn = UIButton(backgroundImage: "common_button_white", title: "登录",fontSzie:14, tagart: self, action: #selector(btnClick))
        leftBtn.setTitleColor(ORANGE, for: .normal)
        return leftBtn
    }()
    
    /// 注册按钮
    lazy var rightBtn:UIButton = {
        let rightBtn = UIButton(backgroundImage: "common_button_white", title: "注册",fontSzie:14, tagart: self, action: #selector(btnClick))
        rightBtn.setTitleColor(ORANGE, for: .normal)
        return rightBtn
    }()
    
    
}
